<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        fieldset{
            width: 200px;
            margin: 0 auto;
        }
    </style>
</head>
<body>
    <!-- 分组元素 -->
    <fieldset>
        <!-- 分组元素的标题 -->
        <legend>控制页面背景色</legend>
        <div class="btns">
            <button id="red">红色</button>
            <button id="green">绿色</button>
            <button id="blue">蓝色</button>
        </div>
    </fieldset>
    <script>
        //找到全部按钮元素，并分别保存在变量中
        var btnRed = document.getElementById("red");
        var btnGreen = document.getElementById("green");
        var btnBlue = document.getElementById("blue");
        //绑定按钮元素的单击事件
        btnRed.onclick = function(){
            document.body.style.backgroundColor="red";
        }
        btnGreen.onclick = function(){
            document.body.style.backgroundColor="green";
        }
        btnBlue.onclick = function(){
            document.body.style.backgroundColor="blue";
        }
        
    </script>
</body>
</html>